package al.softsynergy.auk.ui.ankesa;

import java.util.List;

import al.softsynergy.auk.AukApplication;
import al.softsynergy.auk.data.containers.AnkesaContainer;
import al.softsynergy.auk.data.entries.Ankesa;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.terminal.UserError;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.Notification;


public class AnkesaSearchPanel extends Panel implements Button.ClickListener{

	private final TextField emri = new TextField("Emri: ");
	private final TextField mbiemri = new TextField("Mbiemri: ");
	
	private AnkesaUI ankesaUI;
	
	private VerticalLayout errorMessage = AnkesaSearchPanel.getErrorLayout();
	
	
	public void reset(){
		this.errorMessage.setVisible(false);
	}
	
	
	public  static VerticalLayout getErrorLayout(){
		VerticalLayout vl = new VerticalLayout();
		vl.setWidth("100%");
		vl.setHeight("20px");
		vl.addStyleName("prova");
		vl.setVisible(false);
		return vl;
		
	}
	
	
	public AnkesaSearchPanel(AnkesaUI ankesaUI) {
		super();
		this.ankesaUI = ankesaUI;

		emri.setMaxLength(30);
		emri.setRequired(true);
		emri.setValidationVisible(false);
		
		mbiemri.setMaxLength(30);
		mbiemri.setRequired(true);
		mbiemri.setValidationVisible(false);
		
		VerticalLayout vl = new VerticalLayout();
		vl.setWidth("210px");
		
		FormLayout formLayout = new FormLayout();
		formLayout.addComponent(emri);
		formLayout.addComponent(mbiemri);
		
		vl.addComponent(formLayout);
		vl.addComponent(errorMessage);
		Button searchButton = new Button("Kerko");
		searchButton.addListener(this);
		vl.addComponent(searchButton);
		vl.setComponentAlignment(searchButton, Alignment.TOP_RIGHT);
		
		this.addComponent(vl);
		
	}

	@Override
	public void buttonClick(ClickEvent event) {
		try {
			if (this.validate()){
				String emriVal;
				String mbiemriVal;
				if (emri.getValue() != null && !emri.getValue().toString().trim().equals(""))
					emriVal = "%" + emri.getValue() + "%";
				else
					emriVal = "%";
				
				if (mbiemri.getValue() != null && !mbiemri.getValue().toString().trim().equals(""))
					mbiemriVal = "%" + mbiemri.getValue() + "%";
				else
					mbiemriVal = "%";
				
				List<Ankesa> ankesaList = this.ankesaUI.getAukBean().queryAnkesa(emriVal, mbiemriVal);
				
				if (ankesaList == null || ankesaList.isEmpty()){
					getWindow().showNotification(
							"Nuk ka te dhena nga kerkimi !",
							Notification.TYPE_WARNING_MESSAGE);
				}
				this.ankesaUI.setDataSource(new AnkesaContainer(ankesaList));
				ankesaUI.showAnkesaList();
			}

		} catch (Exception  e) {
	
		}
	}
	public boolean validate(){
		try {
			if (! emri.isValid() && !mbiemri.isValid())
				throw new InvalidValueException("Dy fushat nuk mund te jene bosh njekohesisht");

		} catch (InvalidValueException e) {
			errorMessage.setVisible(true);
			errorMessage.removeAllComponents();
			Label lab = new Label(e.getMessage());
			errorMessage.addComponent(lab);
			return false;
		}
		
		return true;

	}
	
	
}
